package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlTransient;
import valueObject.NodeVO;

/**
 *
 * @author David
 */
@Entity
@Table(name = "node")
@NamedQueries({
    @NamedQuery(name="findNodesByParent", query="SELECT n FROM Node n WHERE n.nodoPadre.idNode = :padre"),
    @NamedQuery(name="findNodesByCourse", query="SELECT n FROM Node n WHERE n.courseidCourse.idCourse = :idCurso")
})
public class Node implements Serializable, JPAEntity<NodeVO> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idNode")
    private Integer idNode;
    @Basic(optional = false)
    @Column(name = "type")
    private String type;
    @Column(name = "description")
    private String description;
    @Basic(optional = false)
    @Lob
    @Column(name = "data")
    private String data;
    @JoinColumn(name = "course_idCourse", referencedColumnName = "idCourse")
    @ManyToOne(optional = true)
    private Course courseidCourse;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "nodoPadre")
    private Collection<Node> nodeCollection;
    @JoinColumn(name = "nodoPadre", referencedColumnName = "idNode")
    @ManyToOne(optional = false)
    private Node nodoPadre;

    public Node() {
    }

    public Node(Integer idNode) {
        this.idNode = idNode;
    }

    public Node(Integer idNode, String type, String data) {
        this.idNode = idNode;
        this.type = type;
        this.data = data;
    }

    public Integer getIdNode() {
        return idNode;
    }

    public void setIdNode(Integer idNode) {
        this.idNode = idNode;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public Course getCourseidCourse() {
        return courseidCourse;
    }

    public void setCourseidCourse(Course courseidCourse) {
        this.courseidCourse = courseidCourse;
    }

    @XmlTransient
    public Collection<Node> getNodeCollection() {
        return nodeCollection;
    }

    public void setNodeCollection(Collection<Node> nodeCollection) {
        this.nodeCollection = nodeCollection;
    }

    public Node getNodoPadre() {
        return nodoPadre;
    }

    public void setNodoPadre(Node nodoPadre) {
        this.nodoPadre = nodoPadre;
    }

    @Override
    public NodeVO toValueObject() {
        NodeVO nodeVO = new NodeVO();
        nodeVO.setCourse(courseidCourse.toValueObject());
        nodeVO.setContent(data);
        nodeVO.setDescription(description);
        nodeVO.setId(idNode);
        ArrayList<NodeVO> nodeVOs = new ArrayList<NodeVO>();
        if(nodeCollection != null){
            for(Node n : nodeCollection){
                nodeVOs.add(n.toValueObject());
            }
        }
        nodeVO.setNodes(nodeVOs);
        if(nodoPadre != null)
           nodeVO.setParentNode(nodoPadre.toValueObject());
        nodeVO.setType(type);
        return nodeVO;
    }
    
}